package cc.sjfx.sharedoc.widget.view;

import cn.mandroid.frame.toast.MLog;
import cc.sjfx.sharedoc.R;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ShowSizeEditText extends RelativeLayout {
	private EditText contentEdit;
	private TextView sizeText;
	private TextView maxSizeText;
	private int maxSize = 200;
	String content = "";

	public ShowSizeEditText(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		// TODO Auto-generated constructor stub
		LayoutInflater.from(context).inflate(R.layout.edittext_showsize, this);
		contentEdit = (EditText) findViewById(R.id.contentEdit);
		sizeText = (TextView) findViewById(R.id.sizeText);
		maxSizeText = (TextView) findViewById(R.id.maxSizeText);
		maxSizeText.setText(maxSize + "");

	}

	public void setGetContent(final GetContent getContent) {
		contentEdit.addTextChangedListener(new TextWatcher() {
			@Override
			public void afterTextChanged(Editable s1) {
				// TODO Auto-generated method stub
				MLog.i("aft " + s1.toString());
				String s = s1.toString();
				if (s.length() > maxSize) {
					s = ((String) s).substring(0, maxSize );
					contentEdit.setText(s);
					MLog.i("si" + s.length());
				} else {
					sizeText.setText(s.length() + "");
					getContent.content(s + "");
					contentEdit.setSelection(s.length());
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				MLog.i("bef " + s);
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				MLog.i("ont " + s);

			}
		});

	}

	public void setMaxSize(int size) {
		maxSize = size;
		maxSizeText.setText(maxSize + "");
	}

	public interface GetContent {
		public void content(String content);
	}
}
